package 面试经典150题.哈希表;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tmh
 * @date 2024/6/9 20:51
 * @description 自己想的题目解法，击败了20%的人，能过了就行
 */
public class T383赎金信 {
    public static void main(String[] args) {
        String ransomNote = "aa";
        String magazine = "aab";
        boolean result = canConstruct(ransomNote, magazine);
        System.out.println(result);
    }

    public static boolean canConstruct(String ransomNote, String magazine) {
        if (ransomNote.length() > magazine.length()) {
            return false;
        }
        Map<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < ransomNote.length(); i++) {
            map.put(ransomNote.charAt(i), map.getOrDefault(ransomNote.charAt(i), 0) + 1);
        }
        for (int i = 0; i < magazine.length(); i++) {
            map.put(magazine.charAt(i), map.getOrDefault(magazine.charAt(i), 0) - 1);
        }
        List<Integer> result = map.values().stream().filter(num -> num > 0).collect(Collectors.toList());
        return result.size() > 0 ? false : true;
    }
}
